name: "Check Docs"
# https://github.com/marketplace/actions/sphinx-build

on: # Trigger the workflow on push or pull request, but only for the master branch
  push:
    branches: [master]
  pull_request: {}

env:
  FREEZE_REQUIREMENTS: 1
  TORCH_URL: https://download.pytorch.org/whl/cpu/torch_stable.html
  TRANSFORMERS_CACHE: _hf_cache

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-${{ github.head_ref }}
  cancel-in-progress: ${{ ! (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/release/')) }}

defaults:
  run:
    shell: bash

jobs:

  make-docs:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true
    - uses: actions/setup-python@v4
      with:
        python-version: 3.8

    - name: Cache pip
      uses: actions/cache@v3
      with:
        path: ~/.cache/pip  # this is specific for Ubuntu
        key: pip-${{ hashFiles('requirements/*.txt') }}
        restore-keys: pip-
    - name: Install dependencies
      run: |
        sudo apt-get update --fix-missing
        # install Texlive, see https://linuxconfig.org/how-to-install-latex-on-ubuntu-20-04-focal-fossa-linux
        sudo apt-get install -y cmake pandoc texlive-latex-extra dvipng texlive-pictures
        pip --version
        pip install -e . -r requirements/docs.txt -f $TORCH_URL
        pip list

    - name: Cache transformers
      uses: actions/cache@v3
      with:
        path: ${{ env.TRANSFORMERS_CACHE }}
        key: cache-transformers

    - name: Make Documentation
      working-directory: docs/
      run: make html --debug --jobs 2 SPHINXOPTS="-W --keep-going"

    - name: Upload built docs
      uses: actions/upload-artifact@v3
      with:
        name: docs-results-${{ github.sha }}
        path: docs/build/html/


  test-docs:
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true
    - uses: actions/setup-python@v4
      with:
        python-version: 3.8

    - name: Cache pip
      uses: actions/cache@v3
      with:
        path: ~/.cache/pip  # this is specific for Ubuntu
        key: pip-${{ hashFiles('requirements/*.txt') }}
        restore-keys: pip-

    - name: Install dependencies
      run: |
        sudo apt-get update --fix-missing
        sudo apt-get install -y cmake pandoc libsndfile1
        pip --version
        pip install -e '.[all,test]' -r requirements/docs.txt -f $TORCH_URL
        pip list

    - name: Cache transformers
      uses: actions/cache@v3
      with:
        path: ${{ env.TRANSFORMERS_CACHE }}
        key: cache-transformers

    - name: Test Documentation
      working-directory: docs/
      env:
        SPHINX_MOCK_REQUIREMENTS: 0
        FIFTYONE_DO_NOT_TRACK: true
        FLASH_TESTING: 1
      run: make doctest
